package view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import utils.CommonUtils;
import yk.bwie.liu_news.R;

/**
 * Created by asus on 2016/11/1.
 */
public class ViewPagerIndicator extends HorizontalScrollView implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager viewPager;
    private LinearLayout lin;

    public ViewPagerIndicator(Context context) {
        super(context);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewPagerIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public void setViewPager(ViewPager viewPager){
        this.viewPager = viewPager;
        viewPager.setOnPageChangeListener(this);

        lin = new LinearLayout(getContext());
        PagerAdapter adapter = viewPager.getAdapter();
        for (int i = 0; i < adapter.getCount(); i++) {
            View view = CommonUtils.inflate(R.layout.line_item);
            view.setOnClickListener(this);
            TextView tv_indicator_line = (TextView) view.findViewById(R.id.tv_indicator_line);
            TextView tv_indicator_title = (TextView) view.findViewById(R.id.tv_indicator_title);
            if (i == 0){
                tv_indicator_line.setVisibility(View.VISIBLE);
                tv_indicator_title.setTextColor(Color.GREEN);
            }else {
                tv_indicator_title.setTextColor(Color.GRAY);
                tv_indicator_line.setVisibility(View.INVISIBLE);
            }
            tv_indicator_title.setText(adapter.getPageTitle(i));
            LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutparams.setMargins(CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5),CommonUtils.dip2px(5));
            lin.addView(view,layoutparams);
        }
        this.addView(lin);
    }

    @Override
    public void onClick(View v) {
        for (int i = 0; i < lin.getChildCount(); i++) {
            View childAt = lin.getChildAt(i);
            TextView title = (TextView) childAt.findViewById(R.id.tv_indicator_title);
            TextView line = (TextView) childAt.findViewById(R.id.tv_indicator_line);
            if (v == lin.getChildAt(i)){
                viewPager.setCurrentItem(i);
                line.setVisibility(View.VISIBLE);
                title.setTextColor(Color.GREEN);
            }else {
                line.setVisibility(View.INVISIBLE);
                title.setTextColor(Color.GRAY);
            }
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < lin.getChildCount(); i++) {
            View childAt = lin.getChildAt(i);
            TextView title = (TextView) childAt.findViewById(R.id.tv_indicator_title);
            TextView line = (TextView) childAt.findViewById(R.id.tv_indicator_line);
            if (position == i){
                this.scrollTo((int) childAt.getX()-lin.getChildAt(0).getWidth(),0);
                line.setVisibility(View.VISIBLE);
                title.setTextColor(Color.GREEN);
            }else {
                line.setVisibility(View.INVISIBLE);
                title.setTextColor(Color.GRAY);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
